﻿using Prism.Commands;
using Prism.Events;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WPF_UI_Lightningchart.Events;
using WPF_UI_Lightningchart.Helper;
using WPF_UI_Lightningchart.Services;

namespace WPF_UI_Lightningchart.ViewModels
{
    public class MainWindowViewModel : ReactiveObject
    {
        private readonly LayoutManager _layoutManager = new LayoutManager();
        private readonly IEventAggregator _eventAggregator;
        public ObservableCollection<SensorViewModel> Sensors { get; set; }
        [Reactive]public List<string> Layouts { get; set; }
        
        public DelegateCommand StopUpdate { get; }
        public MainWindowViewModel(IEventAggregator eventAggregator)
        {
            
            _eventAggregator = eventAggregator;

            Layouts = new List<string>();
            // 订阅 LayoutEvent 事件

            LoadSensorData();
            StopUpdate = new DelegateCommand(StopUpdateData);

            _eventAggregator.GetEvent<LayoutEvent>().Subscribe(LoadLayoutList);
            LoadLayoutList();
        }

        private void LoadLayoutList()
        {
            Layouts = _layoutManager.GetSavedLayouts();
        }

        private void StopUpdateData()
        {
            Sensors[0].StopData();
        }

        private void LoadSensorData()
        {
            Sensors = new ObservableCollection<SensorViewModel>
                {
                    new SensorViewModel("传感器001"),
                    new SensorViewModel("传感器002"),
                    new SensorViewModel("传感器003"),
                    new SensorViewModel("传感器004"),
                    new SensorViewModel("传感器005"),
                    new SensorViewModel("传感器006"),
                    new SensorViewModel("传感器007"),
                    new SensorViewModel("传感器008"),
                    new SensorViewModel("传感器009"),
                    new SensorViewModel("传感器010"),
                };
        }
    }
}
